{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e74b8189",
   "metadata": {},
   "outputs": [
    {
     "ename": "JSONDecodeError",
     "evalue": "Expecting ',' delimiter: line 1 column 127133 (char 127132)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_13288/4080956785.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mreponse\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'data'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"'\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'\"'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mreponse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcatch_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m \u001b[1;31m# # 获取数据的键值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;31m# data.keys()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_13288/4080956785.py\u001b[0m in \u001b[0;36mcatch_data\u001b[1;34m()\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0mreponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[1;31m#返回数据字典\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m     \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mreponse\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'data'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"'\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'\"'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     12\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mreponse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcatch_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\python-3.9.2\\lib\\json\\__init__.py\u001b[0m in \u001b[0;36mloads\u001b[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[0;32m    344\u001b[0m             \u001b[0mparse_int\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mparse_float\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32mand\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    345\u001b[0m             parse_constant is None and object_pairs_hook is None and not kw):\n\u001b[1;32m--> 346\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0m_default_decoder\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    347\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mcls\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    348\u001b[0m         \u001b[0mcls\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mJSONDecoder\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\python-3.9.2\\lib\\json\\decoder.py\u001b[0m in \u001b[0;36mdecode\u001b[1;34m(self, s, _w)\u001b[0m\n\u001b[0;32m    335\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    336\u001b[0m         \"\"\"\n\u001b[1;32m--> 337\u001b[1;33m         \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_decode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0m_w\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    338\u001b[0m         \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_w\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    339\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\python-3.9.2\\lib\\json\\decoder.py\u001b[0m in \u001b[0;36mraw_decode\u001b[1;34m(self, s, idx)\u001b[0m\n\u001b[0;32m    351\u001b[0m         \"\"\"\n\u001b[0;32m    352\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 353\u001b[1;33m             \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscan_once\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    354\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    355\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mJSONDecodeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Expecting value\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mJSONDecodeError\u001b[0m: Expecting ',' delimiter: line 1 column 127133 (char 127132)"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import json\n",
    "import requests\n",
    "from datetime import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "def catch_data():\n",
    "    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'\n",
    "    reponse = requests.get(url=url).json()\n",
    "    #返回数据字典\n",
    "    data = json.loads(reponse['data'].replace(\"'\",'\"'))\n",
    "    return reponse\n",
    "data = catch_data()\n",
    "# # 获取数据的键值\n",
    "# data.keys()\n",
    "# # 获取数据的最后更新时间\n",
    "# lastUpdateTime = data['lastUpdateTime']\n",
    "# # lastUpdateTime\n",
    "# chinaTotal = data['chinaTotal']\n",
    "# # chinaTotal\n",
    "# chinaAdd = data['chinaAdd']\n",
    "# # 累计确诊总计\n",
    "# # print(chinaTotal)\n",
    "# # # 较昨日新增\n",
    "# print(chinaAdd)\n",
    "#da = data['data']\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7de9f7b5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bafdb314",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c0ad5197",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'较昨日新增确诊': 6,\n",
       " '较昨日康复': 54,\n",
       " '境外新增': 15,\n",
       " '无症状感染者': 39,\n",
       " '本土新增确诊': 39,\n",
       " '新增死亡': 1,\n",
       " '累计新增确诊': 61}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newadd=chinaAdd\n",
    "# newadd['累计新增确诊']=newadd.pop('confirm')\n",
    "# newadd['较昨日新增确诊']=newadd.pop('nowConfirm')\n",
    "# newadd['较昨日康复']=newadd.pop('heal')\n",
    "# newadd['境外新增']=newadd.pop('importedCase')\n",
    "# newadd['无症状感染者']=newadd.pop('noInfect')\n",
    "# newadd['较昨日新增确诊']=newadd.pop('IocalConfirmH5')\n",
    "# newadd['新增死亡']=newadd.pop('dead')\n",
    "# newadd['本土新增确诊']=newadd.pop('localConfirmH5')\n",
    "# del newadd['suspect']\n",
    "# newadd.pop('nowSevere')\n",
    "# newadd.pop('localConfirm')\n",
    "# newadd.pop('noInfectH5')\n",
    "newadd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9e95fed0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"71696a8aea034ad7a79cfc8d994d49bc\" style=\"width:720px; height:320px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_71696a8aea034ad7a79cfc8d994d49bc = echarts.init(\n",
       "                    document.getElementById('71696a8aea034ad7a79cfc8d994d49bc'), 'white', {renderer: 'canvas'});\n",
       "                var option_71696a8aea034ad7a79cfc8d994d49bc = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u8f83\\u6628\\u65e5\\u65b0\\u589e\\u786e\\u8bca\",\n",
       "                    \"value\": 6\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8f83\\u6628\\u65e5\\u5eb7\\u590d\",\n",
       "                    \"value\": 54\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5883\\u5916\\u65b0\\u589e\",\n",
       "                    \"value\": 15\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u65e0\\u75c7\\u72b6\\u611f\\u67d3\\u8005\",\n",
       "                    \"value\": 39\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u672c\\u571f\\u65b0\\u589e\\u786e\\u8bca\",\n",
       "                    \"value\": 39\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u65b0\\u589e\\u6b7b\\u4ea1\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7d2f\\u8ba1\\u65b0\\u589e\\u786e\\u8bca\",\n",
       "                    \"value\": 61\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u8f83\\u6628\\u65e5\\u65b0\\u589e\\u786e\\u8bca\",\n",
       "                \"\\u8f83\\u6628\\u65e5\\u5eb7\\u590d\",\n",
       "                \"\\u5883\\u5916\\u65b0\\u589e\",\n",
       "                \"\\u65e0\\u75c7\\u72b6\\u611f\\u67d3\\u8005\",\n",
       "                \"\\u672c\\u571f\\u65b0\\u589e\\u786e\\u8bca\",\n",
       "                \"\\u65b0\\u589e\\u6b7b\\u4ea1\",\n",
       "                \"\\u7d2f\\u8ba1\\u65b0\\u589e\\u786e\\u8bca\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"type\": \"scroll\",\n",
       "            \"show\": true,\n",
       "            \"right\": \"right\",\n",
       "            \"orient\": \"vertical\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"2021-11-10 09:38:39\\u8f83\\u6628\\u65e5\\u65b0\\u589e\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_71696a8aea034ad7a79cfc8d994d49bc.setOption(option_71696a8aea034ad7a79cfc8d994d49bc);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x272790302b0>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Pie\n",
    "import pyecharts.options as opts\n",
    "import time,datetime\n",
    "time=datetime.datetime.fromtimestamp(time.time())\n",
    "str1 = time.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "(\n",
    "    Pie(init_opts=opts.InitOpts(width='720px',height='320px'))\n",
    "    .add(series_name='', data_pair=[list(z) for z in zip(newadd.keys(), newadd.values())])\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=str1+'较昨日新增'),\n",
    "                    legend_opts=opts.LegendOpts(type_='scroll',pos_right='right',orient='vertical'))\n",
    "\n",
    ").render_notebook()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0947ff29",
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取全国各地的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ff275e3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>province</th>\n",
       "      <th>city</th>\n",
       "      <th>total</th>\n",
       "      <th>today</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>台湾</td>\n",
       "      <td>地区待确认</td>\n",
       "      <td>{'nowConfirm': 1861, 'confirm': 16451, 'suspec...</td>\n",
       "      <td>{'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>黑河</td>\n",
       "      <td>{'nowConfirm': 244, 'confirm': 275, 'suspect':...</td>\n",
       "      <td>{'confirm': 4, 'confirmCuts': 0, 'isUpdated': ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>{'nowConfirm': 6, 'confirm': 504, 'suspect': 0...</td>\n",
       "      <td>{'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>境外输入</td>\n",
       "      <td>{'nowConfirm': 2, 'confirm': 404, 'suspect': 0...</td>\n",
       "      <td>{'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>牡丹江</td>\n",
       "      <td>{'nowConfirm': 0, 'confirm': 36, 'suspect': 0,...</td>\n",
       "      <td>{'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  province   city                                              total  \\\n",
       "0       台湾  地区待确认  {'nowConfirm': 1861, 'confirm': 16451, 'suspec...   \n",
       "1      黑龙江     黑河  {'nowConfirm': 244, 'confirm': 275, 'suspect':...   \n",
       "2      黑龙江    哈尔滨  {'nowConfirm': 6, 'confirm': 504, 'suspect': 0...   \n",
       "3      黑龙江   境外输入  {'nowConfirm': 2, 'confirm': 404, 'suspect': 0...   \n",
       "4      黑龙江    牡丹江  {'nowConfirm': 0, 'confirm': 36, 'suspect': 0,...   \n",
       "\n",
       "                                               today  \n",
       "0  {'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...  \n",
       "1  {'confirm': 4, 'confirmCuts': 0, 'isUpdated': ...  \n",
       "2  {'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...  \n",
       "3  {'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...  \n",
       "4  {'confirm': 0, 'confirmCuts': 0, 'isUpdated': ...  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据明细，数据结构比较复杂，一步一步打印出来看，先明白数据结构\n",
    "areaTree = data['areaTree']\n",
    "# 国内数据,获取每个省份的数据信息\n",
    "china_data = areaTree[0]['children']\n",
    "# 新建一个列表\n",
    "china_list = []\n",
    "for a in range(len(china_data)):\n",
    "#     省份名称\n",
    "    province = china_data[a]['name']\n",
    "#     获取省份所在的地区\n",
    "    province_list = china_data[a]['children']\n",
    "#     遍历省份所在的地区\n",
    "    for b in range(len(province_list)):\n",
    "        city = province_list[b]['name']\n",
    "        total = province_list[b]['total']\n",
    "        today = province_list[b]['today']\n",
    "        china_dict = {}\n",
    "        china_dict['province'] = province\n",
    "        china_dict['city'] = city\n",
    "        china_dict['total'] = total\n",
    "        china_dict['today'] = today\n",
    "        china_list.append(china_dict)\n",
    "\n",
    "china_data = pd.DataFrame(china_list)\n",
    "china_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "19e03fc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>province</th>\n",
       "      <th>city</th>\n",
       "      <th>confirm</th>\n",
       "      <th>dead</th>\n",
       "      <th>heal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>台湾</td>\n",
       "      <td>地区待确认</td>\n",
       "      <td>16451</td>\n",
       "      <td>848</td>\n",
       "      <td>13742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>黑河</td>\n",
       "      <td>275</td>\n",
       "      <td>0</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>哈尔滨</td>\n",
       "      <td>504</td>\n",
       "      <td>4</td>\n",
       "      <td>494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>境外输入</td>\n",
       "      <td>404</td>\n",
       "      <td>0</td>\n",
       "      <td>402</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>黑龙江</td>\n",
       "      <td>牡丹江</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  province   city  confirm  dead   heal\n",
       "0       台湾  地区待确认    16451   848  13742\n",
       "1      黑龙江     黑河      275     0     31\n",
       "2      黑龙江    哈尔滨      504     4    494\n",
       "3      黑龙江   境外输入      404     0    402\n",
       "4      黑龙江    牡丹江       36     0     36"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义数据处理函数\n",
    "def confirm(x):\n",
    "    confirm = eval(str(x))['confirm']\n",
    "    return confirm\n",
    "def dead(x):\n",
    "    dead = eval(str(x))['dead']\n",
    "    return dead\n",
    "def heal(x):\n",
    "    heal =  eval(str(x))['heal']\n",
    "    return heal\n",
    "# 函数映射\n",
    "china_data['confirm'] = china_data['total'].map(confirm)\n",
    "china_data['dead'] = china_data['total'].map(dead)\n",
    "china_data['heal'] = china_data['total'].map(heal)\n",
    "china_data = china_data[[\"province\",\"city\",\"confirm\",\"dead\",\"heal\"]]\n",
    "china_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9d50eb47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   province  confirm\n",
      "0        上海     2768\n",
      "1        云南     1610\n",
      "2       内蒙古      613\n",
      "3        北京     1177\n",
      "4        台湾    16451\n",
      "5        吉林      580\n",
      "6        四川     1255\n",
      "7        天津      509\n",
      "8        宁夏      122\n",
      "9        安徽     1008\n",
      "10       山东      993\n",
      "11       山西      263\n",
      "12       广东     3248\n",
      "13       广西      358\n",
      "14       新疆      981\n",
      "15       江苏     1603\n",
      "16       江西      953\n",
      "17       河北     1440\n",
      "18       河南     1603\n",
      "19       浙江     1497\n",
      "20       海南      190\n",
      "21       湖北    68309\n",
      "22       湖南     1197\n",
      "23       澳门       77\n",
      "24       甘肃      344\n",
      "25       福建     1314\n",
      "26       西藏        1\n",
      "27       贵州      159\n",
      "28       辽宁      533\n",
      "29       重庆      610\n",
      "30       陕西      701\n",
      "31       青海       30\n",
      "32       香港    12369\n",
      "33      黑龙江     1970\n"
     ]
    }
   ],
   "source": [
    "# 提取我们需要的数据\n",
    "area_data = china_data.groupby(\"province\")[\"confirm\"].sum().reset_index()\n",
    "area_data.columns = [\"province\",\"confirm\"]\n",
    "print(area_data )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "8ff6767d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\阚其禄\\\\Desktop\\\\新建文件夹\\\\疫情防控.html'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Pie\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.charts import Page\n",
    "import time,datetime\n",
    "time=datetime.datetime.fromtimestamp(time.time())\n",
    "str1 = time.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "page=Page()\n",
    "pie=(\n",
    "    Pie(init_opts=opts.InitOpts(width='720px',height='320px'))\n",
    "    .add(series_name='', data_pair=[list(z) for z in zip(newadd.keys(), newadd.values())])\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=str1+'较昨日新增'),\n",
    "                    legend_opts=opts.LegendOpts(type_='scroll',pos_right='right',orient='vertical'))\n",
    "\n",
    ")\n",
    "page.add(pie)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "from pyecharts.charts import Map #动态图路径\n",
    "import pyecharts.options as opts\n",
    "map1=(\n",
    "    Map()\n",
    "    .add(\"\",[list(z) for z in zip(list(area_data[\"province\"]), list(area_data[\"confirm\"]))], \"china\",is_map_symbol_show=False)#is_map_symbol视觉用\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"2021年中国各地区总确诊人数地图\"),visualmap_opts=opts.VisualMapOpts(is_piecewise=True,\n",
    "                pieces = [\n",
    "                    \n",
    "                        {\"min\": 5000 , \"label\": '>5000',\"color\": \"#893448\"}, #不指定 max，表示 max 为无限大\n",
    "                        {\"min\": 1000, \"max\": 4999, \"label\": '1000-4999',\"color\" : \"#ff585e\" },\n",
    "                        {\"min\": 500, \"max\": 999, \"label\": '500-1000',\"color\": \"#fb8146\"},\n",
    "                        {\"min\": 101, \"max\": 499, \"label\": '101-499',\"color\": \"#ffA500\"},\n",
    "                        {\"min\": 10, \"max\": 100, \"label\": '10-100',\"color\": \"#ffb248\"},\n",
    "                        {\"min\": 0, \"max\": 9, \"label\": '0-9',\"color\" : \"#fff2d1\" }]))\n",
    "\n",
    ")\n",
    "page.add(map1)\n",
    "\n",
    "page.render('疫情防控.html')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a7c99a2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Page"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
